import ResManager from "../../core/ResManager";
import Singleton from "../../net/Singleton";
import { Environment } from "./CFCommon";

export class BattleTest extends Singleton {
    ownerMap: {}
    enemyMap: {}
    heroSkillMap: {}
    isFighting: boolean = false;

    //英雄id
    heros: Array<number> = [];

    init() {
        this.heros = [
            10540505,
            10540601,
            10530504,
            10550702,
            10550204,
            10520202,
            10540206,
            10540107,
            10530803,
            10530808,
            10530603,
            10520304,
            10520403,
            10540407,
            10530501,
            10540703,
            10540705,
            10540704,
            10541008,
            10541001,
            10550103,
            10540106,
            10520101,
            10530806,
            10550605,
            10520110,
            10520209,
            10540210,
            10550606,
            10540507,
            10540709,
            10551009,
            10530510,
            10550310,
            10550202,
            10550101,
            10550809,
            10540802,
            10550105,
            10550401,
            10550104,
            10550507,
            10550803,
            10540301,
            10550405,
            10540405,
            10540304,
            10540604,
            10550504,
            10550308,
            10550707,
            10550309,
            10530102,
            10540102,
            10530902,
            10530202,
            10530109];
        this.ownerMap = {
            1: 10520101,
            2: 10530902,
            3: 10520403,
            4: 10520304,
            // 5:10540107
            5: 10550204
        }
        this.enemyMap = {
            1: 10550809,
            2: 10550202,
            3: 10550702,
            4: 10540601,
            5: 10540505
        }
        this.heroSkillMap = {}
    }

    getHeroConfigData(heroId) {
        let config = ResManager.ins().getRes('config_hero');
        // console.log(config[heroId])
        if (config[heroId]) return config[heroId];
        return null
    }

    getSkillData(heroId) {
        let skillMap = this.getHeroSkills(heroId, [1]);
        let skill1 = skillMap[1][0];
        // console.log(skill1);
        return skill1;
    }

    getSkillValue(heroId) {
        let value = 0;
        let skillMap = this.getHeroSkills(heroId, [1]);
        // console.log("skillMap===",skillMap)
        let skill1 = skillMap[1][0];
        // console.log(skill1);
        let heroData = this.getHeroConfigData(heroId);
        let elementList = skill1.elementList;
        let element1 = elementList[0];
        // console.log(element1);
        let skillElement = this.getSkillElement(element1.id);
        // console.log("skillElement",skillElement);

        //目前只计算影响的hp
        if (skillElement.attrType == 101) {
            if (skillElement.qualityType == 1) {//绝对值
                value = skillElement.result * element1.count;
            } else if (skillElement.qualityType == 2) {//百分比
                value = skillElement.result * Math.floor((element1.count / Environment.globalProBase) * heroData.atk);
            }
        }
        if (skill1.nextSkillId != -1) {
            let config_skill = ResManager.ins().getRes('config_skill');
            let element1 = config_skill[skill1.nextSkillId].elementList[0];
            let skillElement = this.getSkillElement(element1.id);
            // console.log("skillElement2",skillElement);
            if (skillElement.attrType == 101) {
                if (skillElement.qualityType == 1) {//绝对值
                    value += skillElement.result * element1.count;
                } else if (skillElement.qualityType == 2) {//百分比
                    value += skillElement.result * Math.floor((element1.count / Environment.globalProBase) * heroData.atk);
                }
            }
        }

        // console.log("value====",value);
        return value
    }

    getHeroSkills(cardId, limit = []) {
        let config_skill = ResManager.ins().getRes('config_skill');
        let skillMap = {}
        let _skillMap = {}
        if (this.heroSkillMap[cardId]) {
            _skillMap = this.heroSkillMap[cardId]
        }
        else {
            for (let k in config_skill) {
                // console.log("skill===",config_skill[k])
                if (config_skill[k].heroId == cardId && config_skill[k].skillGroup != -1) {
                    if (!_skillMap[config_skill[k].skillGroup]) _skillMap[config_skill[k].skillGroup] = []
                    _skillMap[config_skill[k].skillGroup].push(config_skill[k])
                }
            }
            this.heroSkillMap[cardId] = _skillMap
        }
        for (let i = 0; i < limit.length; i++) {
            if (_skillMap[limit[i]]) {
                skillMap[limit[i]] = _skillMap[limit[i]]
            }
        }
        // console.log(this.heroSkillMap[cardId])
        // console.log(skillMap[1][0])
        return skillMap;
    }

    getSkillElement(elementId) {
        let config_skillElement = ResManager.ins().getRes('config_skillElement');
        return config_skillElement[elementId];
    }


}
